#pragma once

#include <iostream>
#include <thread>         
#include <chrono> 


#include <fmt/core.h>
// #include <glog/logging.h>

#include <queue>
#include <mutex>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
// #include <libavutil/error.h>
#include <libswresample/swresample.h>
}

#include <opencv2/core/core.hpp>

#include "bs_common.h"


class BsPullStreamer
{
public:
    BsPullStreamer();
    ~BsPullStreamer();

    // 用于初始化视频拉流，仅调用一次
    bool setup(std::string name, std::string dncoder);


    // 连接流媒体服务器
    bool connect();
    void start();
    void stop(){pull_running = false;};
    bool is_running(){return pull_running;};
    // 编码视频帧并推流
    static void decodeVideoAndShowThread(void* arg); 



    // 上下文
    AVFormatContext *mFmtCtx = nullptr;
    // 视频帧
    AVCodecContext *mVideoCodecCtx = NULL;
    AVStream *mVideoStream = NULL;

    VideoFrame* mVideoFrame = NULL;
  

    int mVideoIndex = -1;


private:
    std::string name = "";
    int width = -1;
    int height = -1;
    int fps = -1;
    std::string decoder = "";
    int bitrate = -1;



    bool pull_running = false;

    std::thread* mThread;


};